package com.cmcc.aregister.ui.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.cmcc.aregister.R;
import com.cmcc.aregister.cs.constant.ConfigUtilHelper;
import com.cmcc.aregister.cs.model.VoiceExampleModel;

public class VoiceExampleAdapter extends BaseAdapter {

    private List<VoiceExampleModel> list;
    private LayoutInflater layoutInflater;
    private View lastView;// 上一次点击的View
    private int lastPosition;// 上一次点击的Position

    public VoiceExampleAdapter(Context context, List<VoiceExampleModel> list) {
        this.list = list;
        this.layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        VoiceExampleHolder holder = null;
        if (convertView == null) {
            holder = new VoiceExampleHolder();
            convertView = layoutInflater.inflate(R.layout.voice_example_list_item, null);
            holder.iv_icon = (ImageView) convertView.findViewById(R.id.iv_voice_example_icon);
            holder.tv_defvoicetext = (TextView) convertView.findViewById(R.id.tv_voice_example_defvoicetext);
            holder.tv_modelvoicetext = (TextView) convertView.findViewById(R.id.tv_voice_example_modelvoicetext);
            holder.tv_ohter1 = (TextView) convertView.findViewById(R.id.tv_voice_example_other_1);
            holder.tv_ohter2 = (TextView) convertView.findViewById(R.id.tv_voice_example_other_2);
            holder.tv_ohter3 = (TextView) convertView.findViewById(R.id.tv_voice_example_other_3);
            holder.tv_ohter4 = (TextView) convertView.findViewById(R.id.tv_voice_example_other_4);
            holder.tv_ohter5 = (TextView) convertView.findViewById(R.id.tv_voice_example_other_5);
            holder.ly_voice_example_ohter = convertView.findViewById(R.id.ly_voice_example_other);
            convertView.setTag(holder);
        }
        else {
            holder = (VoiceExampleHolder) convertView.getTag();
        }
        VoiceExampleModel model = list.get(position);
        holder.iv_icon.setImageResource(model.icon);
        holder.tv_defvoicetext.setText(model.def_vocie_text);
        holder.tv_modelvoicetext.setText(model.voice_model_text);
        if (ConfigUtilHelper.isNotEmpty(model.example_ohter_1)) {
            holder.tv_ohter1.setVisibility(View.VISIBLE);
            holder.tv_ohter1.setText(model.example_ohter_1);
        }
        else {
            holder.tv_ohter1.setVisibility(View.GONE);
        }
        if (ConfigUtilHelper.isNotEmpty(model.example_ohter_2)) {
            holder.tv_ohter2.setVisibility(View.VISIBLE);
            holder.tv_ohter2.setText(model.example_ohter_2);
        }
        else {
            holder.tv_ohter2.setVisibility(View.GONE);
        }
        if (ConfigUtilHelper.isNotEmpty(model.example_ohter_3)) {
            holder.tv_ohter3.setVisibility(View.VISIBLE);
            holder.tv_ohter3.setText(model.example_ohter_3);
        }
        else {
            holder.tv_ohter3.setVisibility(View.GONE);
        }
        if (ConfigUtilHelper.isNotEmpty(model.example_ohter_4)) {
            holder.tv_ohter4.setVisibility(View.VISIBLE);
            holder.tv_ohter4.setText(model.example_ohter_4);
        }
        else {
            holder.tv_ohter4.setVisibility(View.GONE);
        }
        if (ConfigUtilHelper.isNotEmpty(model.example_ohter_5)) {
            holder.tv_ohter5.setVisibility(View.VISIBLE);
            holder.tv_ohter5.setText(model.example_ohter_5);
        }
        else {
            holder.tv_ohter5.setVisibility(View.GONE);
        }
        return convertView;
    }

    public void showOtherExample(View view, int position) {
        VoiceExampleHolder holder = null;
        if (lastView != null && lastPosition != position) {
            holder = (VoiceExampleHolder) lastView.getTag();
            if (holder != null && holder.ly_voice_example_ohter.getVisibility() == View.VISIBLE) {
                holder.ly_voice_example_ohter.setVisibility(View.GONE);
            }
        }
        lastView = view;
        lastPosition = position;
        holder = (VoiceExampleHolder) view.getTag();
        switch (holder.ly_voice_example_ohter.getVisibility()) {
        case View.VISIBLE:
            holder.ly_voice_example_ohter.setVisibility(View.GONE);
            break;
        case View.GONE:
            holder.ly_voice_example_ohter.setVisibility(View.VISIBLE);
            break;
        }
    }

    private static final class VoiceExampleHolder {
        ImageView iv_icon;
        TextView tv_defvoicetext;
        TextView tv_modelvoicetext;
        TextView tv_ohter1;
        TextView tv_ohter2;
        TextView tv_ohter3;
        TextView tv_ohter4;
        TextView tv_ohter5;
        View ly_voice_example_ohter;
    }
}
